package com.ruoyi.salesInvoice.invoiceStatusMachine.impl;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.sale.mapper.SaleGoodsMapper;
import com.ruoyi.salesInvoice.domain.SalesInvoiceModel;
import com.ruoyi.salesInvoice.domain.SalesInvoiceOrder;
import com.ruoyi.salesInvoice.invoiceStatusMachine.InvoiceService;
import com.ruoyi.salesInvoice.mapper.SalesInvoiceMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 发票状态审核通过
 * @Author Mr.Yi.Zhang
 * @Version 1.0
 */
@Component
public class InvoiceServicePass implements InvoiceService {

    private static final String FLAG = "1";

    @Override
    public String get() {
        return FLAG;
    }

    /** 发票Mapper */
    private SalesInvoiceMapper salesInvoiceMapper;
    /** 销售审核Mapper */
    private AuditMapper auditMapper;
    /** 销货单产品Mapper */
    private SaleGoodsMapper saleGoodsMapper;
    public InvoiceServicePass(
            SalesInvoiceMapper salesInvoiceMapper,
            AuditMapper auditMapper,
            SaleGoodsMapper saleGoodsMapper
    ) {
        this.salesInvoiceMapper = salesInvoiceMapper;
        this.auditMapper = auditMapper;
        this.saleGoodsMapper = saleGoodsMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int execute(SalesInvoiceOrder order) {

        /// ============= 同步销售审核 ============== ///
        Audit auditQueryParams = new Audit();
        auditQueryParams.setOrderId(order.getSalesInvoiceId());
        auditQueryParams.setOrderType("7");
        Audit audit = auditMapper.selectAuditByOrderId(auditQueryParams);
        audit.setAuditStatus("1");
        auditMapper.updateAudit(audit);

        /// ============= 计算销售发票中销货单是否全部生成销售发票 ============== ///
        // 当前发票订单
        SalesInvoiceOrder salesInvoiceOrder = salesInvoiceMapper.selectSalesInvoiceOrderById(order.getSalesInvoiceId());
        // 获取发票关联产品的产品ID集合
        List<SalesInvoiceModel> salesInvoiceModelList = salesInvoiceOrder.getSalesInvoiceModelList();
        List<Long> saleGoodsModelIds = salesInvoiceModelList.stream().map(item -> item.getSaleGoodsModelId()).collect(Collectors.toList());
        // 获取销货单关联的产品ID集合并且去重
        List<SaleGoodsModel> saleGoodsModels = saleGoodsMapper.selectSaleGoodsModelByModelIdList(saleGoodsModelIds);
        List<Long> saleGoodsIds = saleGoodsModels.stream().map(SaleGoodsModel::getSaleGoodsId).distinct().collect(Collectors.toList());
        //判断是否有销货单的开票信息不为0的销货单
        List<Long> saleGoodsList = saleGoodsMapper.selectSaleGoodsIdByList(saleGoodsIds);
        // 转成Set集合,提高contains的效率,比list的O(n)效率高,HashSet是O(1)
        Set<Long> saleGoodsSet = new HashSet<>(saleGoodsList);
        //去除不为0的数据
        List<Long> reduce1 = saleGoodsIds.stream().filter(item -> !saleGoodsSet.contains(item)).collect(toList());
        if (!reduce1.isEmpty()) {
            saleGoodsMapper.updateSaleorderOrderIsPays(reduce1);
        }

        // 同步当前销售发票状态
        SalesInvoiceOrder updateData = new SalesInvoiceOrder();
        updateData.setSalesInvoiceId(salesInvoiceOrder.getSalesInvoiceId());
        updateData.setInvoiceStatus("1");
        updateData.setUpdateTime(new Date());
        updateData.setUpdateBy(SecurityUtils.getUsername());
        updateData.setTotalNum(salesInvoiceOrder.getTotalNum());
        return salesInvoiceMapper.updateSalesInvoiceOrder(updateData);
    }
}
